var ws = null,ws1=null,t=2018;

$(function () {
    conWs(ws,"ws://"+window.location.host+"/ws/tweet_show?t=" +t);
    $.ajax({
        url: '/u/logined',
        type: 'get',
        async:false,
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                user = d.user;
                ws1=conWs(ws1,"ws://"+window.location.host+"/ws/tweet_push?userId="+user.id);
            }
        }
    });
})

function conWs(ws,url) {
    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        ws = new WebSocket(url);

        //连接发生错误的回调方法
        ws.onerror = function () {
            setMessageInnerHTML("error");
        };

        //连接成功建立的回调方法
        ws.onopen = function (event) {
            setMessageInnerHTML("open");
        }

        //接收到消息的回调方法
        ws.onmessage = function (event) {
            addTweet(event.data);
        }

        //连接关闭的回调方法
        ws.onclose = function () {
            setMessageInnerHTML("close");
        }

        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function () {
            ws.close();
        }
    } else {
        alert('Not support websocket')
    }
    return ws;
}


//关闭连接
function closeWs() {
    ws.close();
}




function setMessageInnerHTML(html) {
}

/**
 * 动弹
 * @param tweet
 */
function addTweet(tweet) {
    var item = $.parseJSON(tweet);
    if(item.id>0){
        var name = item.username;
        if(item.nickname){
            name = item.nickname;
        }

        $("#tweetList").children().first().before('<li>'+
            '<div class="story-content">'+
            '<div class="mt-story-title js-story-title" story-data-show="true">'+
            '<p class="transition hour-arrow">'+
            '<span class="icon icon-caret js-mt-index-icon"></span>'+
            '</p>'+
            '<ul class="hour-head">'+
            '<li><a href="/u/'+item.username+'" target="_blank"><img class="hour-tx" src="'+item.headimg+'" alt="头像"></li></a>'+
            '<li><a href="/u/'+item.username+'" target="_blank"><p>'+name+'</p></a><p>'+item.insertDate+'</p></li>'+
            '</ul>'+
            '</div>'+
            '<div class="mt-index-info-parent">'+
            '<div class="story-info mt-story-info">'+
            '<p class="story-detail-hide hour-detail-hide mt-index-cont mt-index-cont2 js-mt-index-cont2">'+
            item.content+
            '<a href="/tweet/'+item.id+'" target="_blank" class="mt-index-cont2-a" style="color: #bbb;">[&nbsp评论&nbsp]</a>'+
            '<br><a onclick="tweet_praise('+item.id+')" style="cursor:hand;">' +
            '<i class="icon icon-zan" style="margin-left: 84%;" title="点赞"></i><em>&nbsp;' +
            '<c id="tweetPraiseCount_'+item.id+'">'+0+'</c></em></a>' +
            '</p>'+
            '</div>'+
            '</div>'+
            '</div>'+
            '</li>');
        $("#tweet_content").val("");
    }

}


/**
 * 发送动弹
 */
function send() {
    layui.use('layer', function () {
        var layer = layui.layer;
        var content = $("#tweet_content").val();
        if (!logined()) {
            showLogin();
            return;
        } else if (!content.trim()) {
            layer.msg("不能为空~~~", {icon: 5});
            return;
        } else {
            var message = {"t":t,"userId":user.id,"content":content};
            ws1.send(JSON.stringify(message));

        }
    })

}
